package xyz.kongbai121.common.redis.serializer;

import xyz.kongbai121.common.utils.RedisKeys;
import xyz.kongbai121.common.utils.RedisUtils;
import xyz.kongbai121.modules.sys.entity.DictData;
import xyz.kongbai121.modules.sys.service.SysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author ljq
 * @version 1.0.0
 * @Description
 * @createTime 2021年11月10日 10:00:00
 */
@Slf4j
@Component
public class DictInitCacheRunner {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SysDictDataService dictDataService;


    /**
     * 初始化字典数据
     */
    @PostConstruct
    public void initDictData() {
        this.clearDictData();
        Optional.ofNullable(dictDataService.getAllList()).ifPresent(list -> {
            list.forEach(dictType -> {
                        Map<String, Object> map = dictType.getDataList().stream().collect(Collectors.toMap(k -> {
                            return String.valueOf(k.getDictValue());
                        }, DictData::getDictLabel));

                        String key = getKey(dictType.getDictType());
                        redisUtils.hMSet(key, map);
                    }
            );
        });
        log.info("==>   字典数据初始化缓存完成");
    }

    /**
     * 插入字典数据
     */
    public void setDictData(String dictType) {
        this.setDictData(dictType, "", "");
    }

    /**
     * 插入字典数据
     */
    public void setDictData(String dictType, String dictValue, String dictLabel) {
        redisUtils.hSet(getKey(dictType), dictValue, dictLabel);
    }

    /**
     * 根据dictType获取字典标签
     */
    public Map<String, Object> getDictNameByDictValue(String dictType) {
        if (!"".equals(dictType)) {
            return redisUtils.hGetAll(getKey(dictType));
        }
        return new HashMap<>();
    }

    /**
     * 根据 dictType，dictValue 获取字典标签
     */
    public String getDictNameByDictValue(String dictType, String dictValue) {
        if (!"".equals(dictType) && !"".equals(dictValue)) {
            return (String) redisUtils.hGet(getKey(dictType), dictValue);
        }
        return "";
    }

    /**
     * 清空字典数据
     */
    public void clearDictData() {
        this.clearDictData("", "");
    }

    /**
     * 删除字典数据
     */
    public void clearDictData(String dictType) {
        this.clearDictData(dictType, "");
    }

    /**
     * 删除字典数据
     * 有键值可以直接删除
     * 没有时将字典数据全部清除
     */
    public void clearDictData(String dictType, String dictValue) {
        if (StringUtils.isNotBlank(dictType) && StringUtils.isNotBlank(dictValue)) {
            redisUtils.hDel(getKey(dictType), dictValue);
        } else {
            if (StringUtils.isBlank(dictType)) dictType = "";
            Set<String> keys = redisUtils.keys(getKey(dictType + "*"));
            redisUtils.delete(keys);
        }
    }

    /**
     * 替换key名
     * 有旧数据直接替换
     * 没有时设置新key
     */
    public void renameIfAbsent(String oldKey, String newKey) {
        if (!redisUtils.hGetAll(getKey(oldKey)).isEmpty()) {
            redisUtils.renameIfAbsent(getKey(oldKey), getKey(newKey));
        } else {
            setDictData(newKey);
        }
    }

    /**
     * 获取拼接key
     */
    private String getKey(String dictType) {
        return RedisKeys.getSysDictDataKey(dictType);
    }

}
